﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using ContractManager.Common.Class;
using ContractManager.Common.FormBase;
using ContractManager.BLL.Business;
using ContractManager.Lists.Contracts;
using DevExpress.XtraEditors;
using Stimulsoft.Report.Components;

namespace ContractManager.Reports.TendersDocuments
{
    public partial class FrmBc13 : XtraFormBase
    {
        #region "Variable"
        private Entity.Forms _form = new Entity.Forms();
        private bool _allowEdit;
        private bool _onlyView;
        private bool _allowDelete;
        public List<DateTime> ListLockDate { get; set; }

        #endregion
        #region "Form Construction"
        public FrmBc13()
        {
            InitializeComponent();
        }
        #endregion

        #region "Private Method"

        private void CheckPer(int formNo)
        {
            var item = ClsForms.SelectByUserForm(ClsVariable.RoleNo, formNo);
            if (item == null) return;
            
            _allowEdit = item.AllowEdit;
            _allowDelete = item.AllowDelete;
            _form = ClsForms.SelectByPk(formNo);
            this.ListLockDate = ClsVoucherLock.GetLockByFormNo(formNo);
        }

        

        private void BlankForm()
        {
            txtFromDate.EditValue = null;
            txtToDate.EditValue = null;
            txtMbId.EditValue = null;

            txtMbId.Properties.DataSource = ClsMemberBranch.SelectAll();
            txtMbId.Properties.DisplayMember = "MbName";
            txtMbId.Properties.ValueMember = "MbID";

            txtContract.Properties.DataSource = ClsContracts.GetLookupAll();
            txtContract.Properties.DisplayMember = "ConNo";
            txtContract.Properties.ValueMember = "ConId";
        }

        private void DoPerform()
        {
            string fileName = ReportName;
            string reportName = ClsVariable.GetReportFullPath(fileName);
            string mbId = String.Format("{0}",txtMbId.EditValue);
            int conId = txtContract.EditValue == null ? 0 : Convert.ToInt32(txtContract.EditValue);
            var report = new Stimulsoft.Report.StiReport();
            report.Load(reportName);

            ClsVariable.CreateWaitDialog(ClsVariable.StrCaption, "Vui lòng đợi...");
            ClsVariable.SetWaitDialogCaption("Đang nạp dữ liệu báo cáo...");

            DataTable dtReport = ClsReports.GetReportBc13(txtFromDate.DateTime, txtToDate.DateTime,
                                                          mbId, conId);
            report.RegData(dtReport);
            report.Compile();
            report.CompiledReport.Click += click;
            ClsVariable.CloseWaitDialog();
            // In thông tin đơn vị, địa chỉ

            report["KyBaoCao"] = String.Format("Từ ngày {0} Đến ngày {1}", txtFromDate.DateTime.ToString("dd/MM/yyyy"),
                                               txtToDate.DateTime.ToString("dd/MM/yyyy"));

            report["DoiTuong"] = ClsVariable.IsSelectAllComboBoxEdit(txtMbId)
                ? "Tất cả"
                : String.Format("{0}", txtMbId.Text);

            //report["DoiTuong"] = String.Format("{0}", txtMbId.Text);
           
            ClsVariable.ApplyReportParam(report, ClsVariable.DtParam);
            report.Render(true);
            report.Show(this.MdiParent);
            
            //report.Show();
        }

        private void click(object sender, EventArgs e)
        {
            StiComponent comp = sender as StiComponent;
            if (comp != null && comp.BookmarkValue == null) return;
            if (comp == null) return;
            var ma = (string)comp.BookmarkValue;
            if (ma == null) return;
            string[] arr = ma.Split(';');
            int voucherId = Convert.ToInt32(arr[0]);
            const int sysBillStyleId = 0;
            int formNo = Convert.ToInt32(arr[1]);
            int vcate = Convert.ToInt32(arr[2]);

            CheckPer(formNo);
            if (_allowEdit == false) _onlyView = true;
            if (String.IsNullOrEmpty(_form.Form_Class)) return;
            if (_form.Form_Class.Contains("FrmContracts"))
            {
                var frm = new FrmContractsDetail()
                {
                    MasterId = voucherId,
                    AsSysBillStyleId = sysBillStyleId,
                    AsSysMyCoYearId = ClsVariable.AsSysMyCoYearId,
                    AsPerPersonId = ClsVariable.EmployeeId,
                    FormNo = formNo,
                    FormSysBillStyleIdExport = Convert.ToInt32(_form.Form_SysBillStyleIdExport),
                    FormXtraLayoutTemplate = _form.Form_XtraLayoutTemplate,
                    FormXtraGridLayoutTemplate =_form.Form_XtraGridLayoutTemplate,
                    FormReportLayoutTemplate =_form.Form_ReportLayoutTemplate,
                    FormDetailVatGridLayout =_form.Form_DetailVatGridLayout,

                    VoucherCate = vcate,
                    IsPrintAfterSave = Convert.ToBoolean(_form.Form_IsPrintAfterSave),
                    FormAccountDefault =Convert.ToInt32(_form.FormAccountDefault),
                    FormIsNotValidate =Convert.ToBoolean(_form.FormIsNotValidate),
                    LstLockDate = ListLockDate,
                    AllowDelete = _allowDelete,

                    OnlyView = _onlyView,

                    Text = String.Format("Thông tin {0}", _form.Form_Name),
                    MdiParent = this.MdiParent,

                    WindowState = FormWindowState.Maximized
                };
                frm.Show();
                frm.Focus();
            }

            if (_form.Form_Class.Contains("FrmRevenues"))
            {
                var frm = new FrmRevenuesDetail()
                {
                    MasterId = voucherId,
                    AsSysBillStyleId = sysBillStyleId,
                    AsSysMyCoYearId = ClsVariable.AsSysMyCoYearId,
                    AsPerPersonId = ClsVariable.EmployeeId,
                    FormNo = formNo,
                    FormSysBillStyleIdExport = Convert.ToInt32(_form.Form_SysBillStyleIdExport),
                    FormXtraLayoutTemplate = _form.Form_XtraLayoutTemplate,
                    FormXtraGridLayoutTemplate = _form.Form_XtraGridLayoutTemplate,
                    FormReportLayoutTemplate = _form.Form_ReportLayoutTemplate,
                    FormDetailVatGridLayout = _form.Form_DetailVatGridLayout,

                    VoucherCate = vcate,
                    IsPrintAfterSave = Convert.ToBoolean(_form.Form_IsPrintAfterSave),
                    FormAccountDefault = Convert.ToInt32(_form.FormAccountDefault),
                    FormIsNotValidate = Convert.ToBoolean(_form.FormIsNotValidate),
                    LstLockDate = ListLockDate,
                    AllowDelete = _allowDelete,

                    OnlyView = _onlyView,

                    Text = String.Format("Thông tin {0}", _form.Form_Name),
                    MdiParent = this.MdiParent,

                    WindowState = FormWindowState.Maximized
                };
                frm.Show();
                frm.Focus();
            }

            if (_form.Form_Class.Contains("FrmDocuments"))
            {
                var frm = new FrmDocumentsDetail()
                {
                    MasterId = voucherId,
                    AsSysBillStyleId = sysBillStyleId,
                    AsSysMyCoYearId = ClsVariable.AsSysMyCoYearId,
                    AsPerPersonId = ClsVariable.EmployeeId,
                    FormNo = formNo,
                    FormSysBillStyleIdExport = Convert.ToInt32(_form.Form_SysBillStyleIdExport),
                    FormXtraLayoutTemplate = _form.Form_XtraLayoutTemplate,
                    FormXtraGridLayoutTemplate = _form.Form_XtraGridLayoutTemplate,
                    FormReportLayoutTemplate = _form.Form_ReportLayoutTemplate,
                    FormDetailVatGridLayout = _form.Form_DetailVatGridLayout,

                    VoucherCate = vcate,
                    IsPrintAfterSave = Convert.ToBoolean(_form.Form_IsPrintAfterSave),
                    FormAccountDefault = Convert.ToInt32(_form.FormAccountDefault),
                    FormIsNotValidate = Convert.ToBoolean(_form.FormIsNotValidate),
                    LstLockDate = ListLockDate,
                    AllowDelete = _allowDelete,

                    OnlyView = _onlyView,

                    Text = String.Format("Thông tin {0}", _form.Form_Name),
                    MdiParent = this.MdiParent,

                    WindowState = FormWindowState.Maximized
                };
                frm.Show();
                frm.Focus();
            }

            if (_form.Form_Class.Contains("FrmTenderDocuments"))
            {
                var frm = new FrmDocumentsDetail()
                {
                    MasterId = voucherId,
                    AsSysBillStyleId = sysBillStyleId,
                    AsSysMyCoYearId = ClsVariable.AsSysMyCoYearId,
                    AsPerPersonId = ClsVariable.EmployeeId,
                    FormNo = formNo,
                    FormSysBillStyleIdExport = Convert.ToInt32(_form.Form_SysBillStyleIdExport),
                    FormXtraLayoutTemplate = _form.Form_XtraLayoutTemplate,
                    FormXtraGridLayoutTemplate = _form.Form_XtraGridLayoutTemplate,
                    FormReportLayoutTemplate = _form.Form_ReportLayoutTemplate,
                    FormDetailVatGridLayout = _form.Form_DetailVatGridLayout,

                    VoucherCate = vcate,
                    IsPrintAfterSave = Convert.ToBoolean(_form.Form_IsPrintAfterSave),
                    FormAccountDefault = Convert.ToInt32(_form.FormAccountDefault),
                    FormIsNotValidate = Convert.ToBoolean(_form.FormIsNotValidate),
                    LstLockDate = ListLockDate,
                    AllowDelete = _allowDelete,

                    OnlyView = _onlyView,

                    Text = String.Format("Thông tin {0}", _form.Form_Name),
                    MdiParent = this.MdiParent,

                    WindowState = FormWindowState.Maximized
                };
                frm.Show();
                frm.Focus();
            }
        }

        #endregion

        #region "Form Event"
        private void FrmBc13Load(object sender, EventArgs e)
        {
            BlankForm();
            if (!String.IsNullOrEmpty(FormXtraLayoutTemplate))
            {
                var fileLayout = ClsVariable.GetRestoreLayoutFullPath(FormXtraLayoutTemplate);
                layoutControl.RestoreLayoutFromXml(fileLayout);
            }
        }
        #endregion

        #region "Button Event"
        private void btnOk_Click(object sender, EventArgs e)
        {
            DoPerform();

        }
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion


    }
}
